<?php
class MaterialManager extends CComponent {
	public static function getMaterialRequisitionHistory($materialModelId, $projectId,
		$untilDate = null, $excludeRequisitionId = null) {
		if (!$materialModelId || !$projectId) {
			return '';
		}
		$criteria = new CDbCriteria();
		$criteria->compare('materialModelId', $materialModelId);
		$criteria->compare('projectId', $projectId);
		if ($untilDate) {
			$criteria->compare('submitDate', '<=' . $untilDate);
		}
		if ($excludeRequisitionId) {
			$criteria->compare('materialRequisitionId', '<>' . $excludeRequisitionId);
		}
		$criteria->join = 'INNER JOIN {{material_requisition}} as materialRequisition on materialRequisitionId=materialRequisition.id '
			. 'INNER JOIN {{material_model}} as materialModel on materialModelId=materialModel.id ';
		$criteria->select = 'sum(amountStandardUnit) as totalHistory, materialModel.unit as unit';
		$itemModel = MaterialRequisitionItem::model();
		$commandBuilder = Yii::app()->db->getSchema()->getCommandBuilder();
		$formatter = Yii::app()->format;
		$command = $commandBuilder->createFindCommand($itemModel->getTableSchema(), $criteria);
		$row = $command->queryRow();
		if ($row['totalHistory'] == 0) {
			return '无';
		}
		$history = $formatter->formatAmount($row['totalHistory']) . ' ' . $row['unit'];
		return $history;
	}

	public static function getMaterialRequisitionLast($materialModelId, $projectId,
		$untilDate = null, $excludeRequisitionId = null) {
		if (!$materialModelId || !$projectId) {
			return '';
		}
		$criteria = new CDbCriteria();
		$criteria->compare('materialModelId', $materialModelId);
		$criteria->compare('projectId', $projectId);
		if ($untilDate) {
			$criteria->compare('submitDate', '<=' . $untilDate);
		}
		if ($excludeRequisitionId) {
			$criteria->compare('materialRequisitionId', '<>' . $excludeRequisitionId);
		}
		$criteria->select = '*';
		$criteria->with = array(
			'materialRequisition',
			'materialModel',
			'applicant'
		);
		$criteria->order = 'submitDate DESC';
		$criteria->limit = 1;
		$itemModel = MaterialRequisitionItem::model();
		$formatter = Yii::app()->format;
		$lastItem = $itemModel->find($criteria);
		if (!$lastItem) {
			return '无';
		}
		$lastItemText = sprintf('%s %s%s (%s)', $lastItem->applicant->fullName,
			$formatter->formatAmount($lastItem->amountStandardUnit),
			$lastItem->materialModel->unit, $lastItem->materialRequisition->submitDate);
		return CHtml::link($lastItemText,
			array(
				'materialRequisition/view',
				'id' => $lastItem->materialRequisitionId
			), array(
				'target' => '_blank'
			));
		;
	}

	public static function getMaterialWarehouseInboundHistory($materialModelId, $projectId,
		$untilDate = null, $excludeWarehouseInboundId = null) {
		if (!$materialModelId || !$projectId) {
			return '';
		}
		$criteria = new CDbCriteria();
		$criteria->compare('materialModelId', $materialModelId);
		$criteria->compare('projectId', $projectId);
		if ($untilDate) {
			$criteria->compare('submitDate', '<=' . $untilDate);
		}
		if ($excludeWarehouseInboundId) {
			$criteria->compare('materialWarehouseInboundId', '<>' . $excludeWarehouseInboundId);
		}
		$criteria->join = 'INNER JOIN {{material_warehouse_inbound}} as materialWarehouseInbound on materialWarehouseInboundId=materialWarehouseInbound.id '
			. 'INNER JOIN {{material_model}} as materialModel on materialModelId=materialModel.id ';
		$criteria->select = 'sum(amountStandardUnit) as totalHistory, materialModel.unit as unit';
		$itemModel = MaterialWarehouseInboundItem::model();
		$commandBuilder = Yii::app()->db->getSchema()->getCommandBuilder();
		$formatter = Yii::app()->format;
		$command = $commandBuilder->createFindCommand($itemModel->getTableSchema(), $criteria);
		$row = $command->queryRow();
		if ($row['totalHistory'] == 0) {
			return '无';
		}
		$history = $formatter->formatAmount($row['totalHistory']) . ' ' . $row['unit'];
		return $history;
	}

	public static function getMaterialWarehouseInboundLast($materialModelId, $projectId,
		$untilDate = null, $excludeWarehouseInboundId = null) {
		if (!$materialModelId || !$projectId) {
			return '';
		}
		$criteria = new CDbCriteria();
		$criteria->compare('materialModelId', $materialModelId);
		$criteria->compare('projectId', $projectId);
		if ($untilDate) {
			$criteria->compare('submitDate', '<=' . $untilDate);
		}
		if ($excludeWarehouseInboundId) {
			$criteria->compare('materialWarehouseInboundId', '<>' . $excludeWarehouseInboundId);
		}
		$criteria->select = '*';
		$criteria->with = array(
			'materialWarehouseInbound',
			'materialModel',
		);
		$criteria->order = 'submitDate DESC';
		$criteria->limit = 1;
		$itemModel = MaterialWarehouseInboundItem::model();
		$formatter = Yii::app()->format;
		$lastItem = $itemModel->find($criteria);
		if (!$lastItem) {
			return '无';
		}
		$lastItemText = sprintf('%s %s%s (%s)', $lastItem->materialWarehouseInbound->handler,
			$formatter->formatAmount($lastItem->amountStandardUnit),
			$lastItem->materialModel->unit, $lastItem->materialWarehouseInbound->submitDate);
		return CHtml::link($lastItemText,
			array(
				'materialWarehouseInbound/view',
				'id' => $lastItem->materialWarehouseInboundId
			), array(
				'target' => '_blank'
			));
	}

	public static function getMaterialReceiptHistory($materialModelId, $projectId,
		$untilDate = null, $excludeReceiptId = null) {
		if (!$materialModelId || !$projectId) {
			return '';
		}
		$criteria = new CDbCriteria();
		$criteria->compare('materialModelId', $materialModelId);
		$criteria->compare('projectId', $projectId);
		if ($untilDate) {
			$criteria->compare('receiptDate', '<=' . $untilDate);
		}
		if ($excludeReceiptId) {
			$criteria->compare('materialReceiptId', '<>' . $excludeReceiptId);
		}
		$criteria->join = 'INNER JOIN {{material_receipt}} as materialReceipt on materialReceiptId=materialReceipt.id '
			. 'INNER JOIN {{material_model}} as materialModel on materialModelId=materialModel.id ';
		$criteria->select = 'sum(amountStandardUnit) as totalHistory, materialModel.unit as unit';
		$itemModel = MaterialReceiptItem::model();
		$commandBuilder = Yii::app()->db->getSchema()->getCommandBuilder();
		$formatter = Yii::app()->format;
		$command = $commandBuilder->createFindCommand($itemModel->getTableSchema(), $criteria);
		$row = $command->queryRow();
		if ($row['totalHistory'] == 0) {
			return '无';
		}
		$history = $formatter->formatAmount($row['totalHistory']) . ' ' . $row['unit'];
		return $history;
	}

	public static function getMaterialReceiptLast($materialModelId, $projectId, $untilDate = null,
		$excludeReceiptId = null) {
		if (!$materialModelId || !$projectId) {
			return '';
		}
		$criteria = new CDbCriteria();
		$criteria->compare('materialModelId', $materialModelId);
		$criteria->compare('projectId', $projectId);
		if ($untilDate) {
			$criteria->compare('receiptDate', '<=' . $untilDate);
		}
		if ($excludeReceiptId) {
			$criteria->compare('materialReceiptId', '<>' . $excludeReceiptId);
		}
		$criteria->select = '*';
		$criteria->with = array(
			'materialReceipt',
			'materialModel',
		);
		$criteria->order = 'receiptDate DESC';
		$criteria->limit = 1;
		$itemModel = MaterialReceiptItem::model();
		$formatter = Yii::app()->format;
		$lastItem = $itemModel->find($criteria);
		if (!$lastItem) {
			return '无';
		}
		$lastItemText = sprintf('%s %s%s (%s)', $lastItem->materialReceipt->handler,
			$formatter->formatAmount($lastItem->amountStandardUnit),
			$lastItem->materialModel->unit, $lastItem->materialReceipt->receiptDate);
		return CHtml::link($lastItemText,
			array(
				'materialReceipt/view',
				'id' => $lastItem->materialReceiptId
			), array(
				'target' => '_blank'
			));
	}
}
